package org.zkdn.modules.wlpt.basicinfo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.jeecg.common.api.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.zkdn.modules.wlpt.basicinfo.entity.RoadsidePole;
import org.zkdn.modules.wlpt.basicinfo.mapper.RoadsidePoleMapper;
import org.zkdn.modules.wlpt.basicinfo.service.IRoadsidePoleService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Arrays;
import java.util.List;
import org.apache.commons.collections4.*;
import org.zkdn.modules.wlpt.basicinfo.vo.RoadsidePoleVO;

/**
 * @Description: roadside_pole
 * @Author: jeecg-boot
 * @Date:   2025-06-18
 * @Version: V1.0
 */
@Service
public class RoadsidePoleServiceImpl extends ServiceImpl<RoadsidePoleMapper, RoadsidePole> implements IRoadsidePoleService {

    @Autowired
    RoadsidePoleMapper roadsidePoleMapper;

    @Override
    public List<RoadsidePoleVO> queryPoleWithPositionList(QueryWrapper queryWrapper) {
        return roadsidePoleMapper.queryPoleWithPositionList(queryWrapper);
    }

    @Override
    public IPage<RoadsidePoleVO> queryPageList(IPage<RoadsidePoleVO> page, QueryWrapper queryWrapper) {
        return roadsidePoleMapper.queryPageList(page, queryWrapper);
    }

    @Override
    public Result saveByValidate(RoadsidePole roadsidePole) {
        QueryWrapper<RoadsidePole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("facility_id", roadsidePole.getFacilityId());
        List<RoadsidePole> roadsidePoleList = roadsidePoleMapper.selectList(queryWrapper);
        if (CollectionUtils.isNotEmpty(roadsidePoleList)) {
            return Result.error("杆件ID'" + roadsidePole.getFacilityId() + "'已存在!" );
        }
        save(roadsidePole);
        return Result.OK("添加成功！");
    }

    @Override
    public Result updateByValidate(RoadsidePole roadsidePole) {
        QueryWrapper<RoadsidePole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("facility_id", roadsidePole.getFacilityId());
        queryWrapper.ne("id", roadsidePole.getId());
        List<RoadsidePole> roadsidePoleList = roadsidePoleMapper.selectList(queryWrapper);
        if (CollectionUtils.isNotEmpty(roadsidePoleList)) {
            return Result.error("杆件ID'" + roadsidePole.getFacilityId() + "'已存在!" );
        }
        updateById(roadsidePole);
        return Result.OK("修改成功！");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result deleteById(String id) {
        roadsidePoleMapper.deleteById(id);
        return Result.OK("删除成功！");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result deleteByIds(String ids) {
        roadsidePoleMapper.deleteBatchIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功！");
    }
}
